package com.example.demo.aspect;

import com.example.demo.exception.BusinessException;
import com.example.demo.exception.SystemException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by fangjicai on 2020/6/20.
 */
@Component
@Aspect
public class ExceptionAspect {

    private static final Logger logger= LoggerFactory.getLogger(ExceptionAspect.class);

    @AfterThrowing(pointcut = "within(com.example.demo.controller.*)",throwing = "ex")
    public void handlerException(JoinPoint joinPoint,Exception ex){
        //获取类对象
        Class declaringType = joinPoint.getSignature().getDeclaringType();
        //获取类全限名
        String clazz = declaringType.getCanonicalName();
        String name = joinPoint.getSignature().getName();
        if(ex instanceof BusinessException){
            logger.warn("clazz:{},name:{}",clazz,name,ex);
        }else if(ex instanceof SystemException){
            logger.error("clazz:{},name:{}",clazz,name,ex);
        }
    }
}
